package br.ujr.pocs.activemq.topic;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ProducerTopic {
	
	public static void main(String[] args) {
		
		String url = "tcp://localhost:61616";
    	Connection connection = null;
    	Session session = null;
    	
		try {
			ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
			connection = connectionFactory.createConnection();
			connection.start();
			
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination topic = session.createTopic("TOPIC.FOO");
			MessageProducer messageProducer = session.createProducer(topic);
			messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			
			TextMessage message = session.createTextMessage("Hello Topic");
    		messageProducer.send(message);
    		
    		System.out.println("Message published...");
			
		} catch (JMSException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if ( session != null ) session.close();
				if ( connection != null ) connection.close();
			} catch (JMSException e) {
				throw new RuntimeException(e);
			}
		}
		
	}

}
